package com.ytf.test.ninth;

import java.util.Arrays;

class Processor{
    public String name(){
        return getClass().getSimpleName();
    }
    Object process(Object obj){
        return obj;
    }
}
class UpCase extends Processor{
    @Override
    String process(Object obj) {
        return ((String)obj).toUpperCase();
    }
}
class LowerCase extends Processor{
    @Override
    String process(Object obj) {
        return ((String)obj).toLowerCase();
    }
}
class Splitter extends Processor{
    @Override
    String process(Object obj) {
        return Arrays.toString(((String)obj).split(" "));
    }
    /*
    @Override
    String[] process(Object obj) {
        return ((String)obj).split(" ");
    }*/
}

/**
 * @author: YTF
 * @date: 2025/1/25 15:51
 * @version: 1.0
 * @description:
 */
public class Apply {
    public static void process(Processor processor,Object object){
        System.out.println(processor.name());
        Object process = processor.process(object);
        System.out.println(process);
    }
    public static String s = "Disagreement with beliefs is by definition incorrect";
    public static void main(String[] args) {
        process(new UpCase(),s);
        process(new LowerCase(),s);
        process(new Splitter(),s);
        /**
         * OutPut:
         * UpCase
         * DISAGREEMENT WITH BELIEFS IS BY DEFINITION INCORRECT
         * LowerCase
         * disagreement with beliefs is by definition incorrect
         * Splitter
         * [Disagreement, with, beliefs, is, by, definition, incorrect]
         *
         * 策略设计模式运用：找了个例子更好的理解和运用
         * 假设有一个电商系统，需要根据不同的促销活动计算商品的最终价格。常见的促销活动有满减、打折和返券等。
         * 如果将这些促销计算方式硬编码在代码中，会导致代码冗长且难以维护和扩展。此时，可以使用策略设计模式来解决该问题。
         */

        ShoppingCart shoppingCart = new ShoppingCart();

        // 选择满减策略
        PromotionStrategy fullReduction = new FullReductionStrategy(200, 50);
        shoppingCart.setPromotionStrategy(fullReduction);
        double finalPrice1 = shoppingCart.calculateFinalPrice(250);
        System.out.println("满减后的价格：" + finalPrice1);

        // 选择打折策略
        PromotionStrategy discount = new DiscountStrategy(0.8);
        shoppingCart.setPromotionStrategy(discount);
        double finalPrice2 = shoppingCart.calculateFinalPrice(250);
        System.out.println("打折后的价格：" + finalPrice2);

        // 选择返券策略
        PromotionStrategy rebate = new RebateStrategy(300, 100);
        shoppingCart.setPromotionStrategy(rebate);
        double finalPrice3 = shoppingCart.calculateFinalPrice(350);
        System.out.println("返券后的价格：" + finalPrice3);
    }
}

interface PromotionStrategy {
    double calculateFinalPrice(double originalPrice);
}

class FullReductionStrategy implements PromotionStrategy {
    private double threshold;
    private double reductionAmount;

    public FullReductionStrategy(double threshold, double reductionAmount) {
        this.threshold = threshold;
        this.reductionAmount = reductionAmount;
    }

    @Override
    public double calculateFinalPrice(double originalPrice) {
        if (originalPrice >= threshold) {
            return originalPrice - reductionAmount;
        } else {
            return originalPrice;
        }
    }
}
class DiscountStrategy implements PromotionStrategy {
    private double discountRate;

    public DiscountStrategy(double discountRate) {
        this.discountRate = discountRate;
    }

    @Override
    public double calculateFinalPrice(double originalPrice) {
        return originalPrice * discountRate;
    }
}
class RebateStrategy implements PromotionStrategy {
    private double threshold;
    private double rebateAmount;

    public RebateStrategy(double threshold, double rebateAmount) {
        this.threshold = threshold;
        this.rebateAmount = rebateAmount;
    }

    @Override
    public double calculateFinalPrice(double originalPrice) {
        if (originalPrice >= threshold) {
            System.out.println("返还优惠券：" + rebateAmount + "元");
        }
        return originalPrice;
    }
}
class ShoppingCart {
    private PromotionStrategy promotionStrategy;

    public void setPromotionStrategy(PromotionStrategy promotionStrategy) {
        this.promotionStrategy = promotionStrategy;
    }

    public double calculateFinalPrice(double originalPrice) {
        return promotionStrategy.calculateFinalPrice(originalPrice);
    }
}





